<?php
	class Storage_product_model extends CI_Model {
	    
    var $id;
    var $storage_id;
    var $product_id;
    var $product_code;
    var $quantity;
    var $date_created;
    var $active;

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    // load all Storage
    function loadAll()
    {
        $query = $this->db->get('storage_product');
        return $query->result();
    }    

    // load all storage
    function loadAllV2($limit = false, $from = false,$cond = false)
    {
        $this->db->select('*');
        $this->db->from('storage_product');        

        if(!empty($cond))
        {            
            if(!empty($cond['product_code']) && $cond['product_code'] != 'false')
                $this->db->where('product_code', $cond['product_code']);
        }

        $this->db->limit($limit, $from);    

        $this->db->order_by('id','desc');

        $query = $this->db->get();        

        //echo $this->db->last_query();
        $return = array(
                'data'  =>  $query->result(),
                'count' =>  $this->db->count_all('storage_product'), 
            );                

        if(!empty($cond))
        {
            $return['count'] = $this->loadAllV2Count($cond);
        }
        return $return;
    }

    // load all storage
    function loadAllV3($limit = false, $from = false,$cond = false)
    {
        $this->db->select('storage_product.*');
        $this->db->from('storage_product');
        $this->db->join('products','products.id = storage_product.product_id');        

        if(!empty($cond))
        {            
            if(!empty($cond['product_code']) && $cond['product_code'] != 'false')
            {
                $where = "storage_product.product_code = '".$cond['product_code']."' OR products.slug LIKE '%".$cond['product_code']."%'";                
                $this->db->where($where);
            }
        }

        $this->db->order_by('storage_product.id','desc');

        $this->db->limit($limit, $from);        

        $query = $this->db->get();                      

        //echo $this->db->last_query();
        $return = array(
                'data'  =>  $query->result(),
                'count' =>  $this->db->count_all('storage_product'), 
            );                

        if(!empty($cond))
        {
            $return['count'] = $this->loadAllV3Count($cond);
        }
        return $return;
    }

    // load all storage
    function loadAllV5($limit = false, $from = false,$cond = false)
    {
        $this->db->select('storage_product.*');
        $this->db->from('storage_product');
        $this->db->join('products','products.id = storage_product.product_id');        

        if(!empty($cond))
        {            
            if(!empty($cond['product_code']) && $cond['product_code'] != 'false')
            {
                $where = "storage_product.product_code = '".$cond['product_code']."'";                
                $this->db->where($where);
            }
        }

        $this->db->order_by('storage_product.id','desc');

        $this->db->limit($limit, $from);        

        $query = $this->db->get();                

        //echo $this->db->last_query()."<br>";      

        //echo $this->db->last_query();
        $return = array(
                'data'  =>  $query->result(),
                'count' =>  $this->db->count_all('storage_product'), 
            );                

        if(!empty($cond))
        {
            $return['count'] = $this->loadAllV3Count($cond);
        }
        return $return;
    }

    function loadAllV2Count($cond)
    {
        $this->db->select('*');
        $this->db->from('storage_product');        

        if(!empty($cond))
        {
            if(!empty($cond['product_code']) && $cond['product_code'] != 'false')
                $this->db->where('product_code', $cond['product_code']);
        }        
        
        return $this->db->count_all_results();
    }

    function loadAllV3Count($cond)
    {
        $this->db->select('storage_product.*');
        $this->db->from('storage_product');
        $this->db->join('products','products.id = storage_product.product_id');        

        if(!empty($cond))
        {            
            if(!empty($cond['product_code']) && $cond['product_code'] != 'false')
            {
                $where = "storage_product.product_code = '".$cond['product_code']."' OR products.title LIKE '%".$cond['product_code']."%'";
                $this->db->where($where);
            }
        }        

        return $this->db->count_all_results();
    }

    function getEntry($con,$id)
    {
        $this->db->select('*');
        $this->db->from('storage_product');

        if($con == 'id')
        {
            $this->db->where('id',$id);            
            $query = $this->db->get();
            $this->db->order_by('id','desc');
            return $query->row();
        }
        else if($con == 'storage_id')
        {
            $this->db->where('storage_id',$id);
            $this->db->order_by('id','desc');
            $query = $this->db->get();            
            return $query->result();
        } 
        else if($con == 'product_code')
        {
            $this->db->where('product_code',$id);
            $this->db->order_by('id','desc');
            $query = $this->db->get();            
            return $query->result();
        }                     
    }

    function insertEntry($data)
    {
    	$this->db->insert('storage_product',$data);  
    	return $this->db->insert_id();  	
    }

    function updateEntry($data,$id)
    {
        $this->db->where('id', $id);
        return $this->db->update('storage_product',$data);
    }

    function remove($id)
    {
    	$this->db->where('id', $id);
		$this->db->delete('storage_product'); 
    }

    //////// this function is special for counting size based on code ///////////////////
    function countQtyOfProductSize($code)
    {
        $this->db->select('*');
        $this->db->from('storage_product');
        $this->db->where('product_code', $code);
        $this->db->where('active',1);

        $query = $this->db->get();

        return $query->num_rows();
    }
}